package com.company.project.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.project.common.config.WebMvcConfigurer;
import com.company.project.common.utils.DataResult;
import com.company.project.entity.SysDept;
import com.company.project.entity.SysUser;
import com.company.project.mapper.SysDeptMapper;
import com.company.project.mapper.SysUserMapper;
import com.company.project.service.UserService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.company.project.mapper.TGhszMapper;
import com.company.project.entity.TGhszEntity;
import com.company.project.service.TGhszService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("tGhszService")
public class TGhszServiceImpl extends ServiceImpl<TGhszMapper, TGhszEntity> implements TGhszService {

    @Resource
    private TGhszMapper tGhszMapper;
    @Resource
    private UserService userService;
    @Resource
    private SysUserMapper userMapper;
    @Resource
    private SysDeptMapper sysDeptMapper;

    @Override
    public DataResult infoData() {
        //根据部门查询自己下面所有人员
        SysUser userInfo = userService.getUserInfo();
        SysDept dept = sysDeptMapper.selectById(userInfo.getDeptId());
        List<Object> objects = sysDeptMapper.selectObjs(Wrappers.<SysDept>lambdaQuery().select(SysDept::getId).like(SysDept::getRelationCode, dept.getDeptNo()));
        List<SysUser> sysUsers = userMapper.selectList(Wrappers.<SysUser>lambdaQuery().in(SysUser::getDeptId, objects));
        List<SysDept> deptList = sysDeptMapper.selectList(Wrappers.<SysDept>lambdaQuery().in(SysDept::getId, objects));
        Map<String,Object> map = new HashMap<>();
        map.put("user",sysUsers);
        map.put("dept",deptList);
        return DataResult.success(map);
    }

    @Override
    public IPage<TGhszEntity> pageByEntity(Page page, TGhszEntity tGhsz) {
        return tGhszMapper.pageByEntity(page,tGhsz);
    }

    @Override
    public void sta(List<String> ids) {
        tGhszMapper.updateSta(ids,"0");
    }

    @Override
    public void nosta(List<String> ids) {
        tGhszMapper.updateSta(ids,"1");
    }

    @Override
    public DataResult getDataById(String id) {
        return DataResult.success(tGhszMapper.selectById(id));
    }

    @Override
    public DataResult removeIds(List<String> ids) {
        Long aLong = tGhszMapper.selectCount(Wrappers.<TGhszEntity>lambdaQuery().in(TGhszEntity::getId, ids).ge(TGhszEntity::getKhsl, 1));
        if (aLong>0) {
            return DataResult.fail("删除失败，公海中包含客户消息！");
        } else {
            removeByIds(ids);
        }
        return DataResult.success();
    }
}